import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:provider/provider.dart';
import 'package:xlsk/config/easyLoading.dart';
import 'package:xlsk/routes/xlsk_router.dart';
import 'package:xlsk/store/index.dart';
import 'package:xlsk/view/home.dart';

void main(){
  // 防止热重载报错
  WidgetsFlutterBinding.ensureInitialized();
  Provider.debugCheckInvalidValueType = null;

  XlskRouter.init();
  runApp(
    MultiProvider(
      providers: Store().providers,
      child: new XlskApp(),
    )
  );
}


class XlskApp extends StatelessWidget {
  final routes = XlskRouter.routes;
  DateTime _lastTime = DateTime.utc(2021);

  @override
  Widget build(BuildContext context){
    return new MaterialApp(
        title: '小鹭搜课',
        localizationsDelegates: [
          GlobalCupertinoLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate,
          GlobalMaterialLocalizations.delegate,
        ],
        supportedLocales: [
          const Locale('zh'),
          const Locale('en'),
        ],
        home: WillPopScope(
          child: Home(),
          onWillPop: _isExit,
        ),
        builder: EasyLoading.init(),
    );
  }

  Future<bool> _isExit(){
    if(_lastTime == DateTime.utc(2021) || DateTime.now().difference(_lastTime) > Duration(milliseconds: 2000)){
      _lastTime = DateTime.now();
      EasyLoading.showToast('再按一次退出程序');
      return Future.value(false);
    }else{
      return Future.value(true);
    }
  }
}
